package com.powernode.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.domain.SysRole;
import com.powernode.domain.SysUser;
import com.powernode.service.SysRoleService;
import com.powernode.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;

import java.util.List;

@RequestMapping("/sys/role")
@RestController
@Api(tags = "角色")
public class sysRoleController {
    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysUserService sysUserService;

    /**
     * 管理员列表分页数据
     * @param page
     * @param sysRole
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "管理员列表")
    @PreAuthorize("hasAuthority('sys:user:page')")
    public ResponseEntity<Page<SysRole>> sysUserPage(Page<SysRole> page, SysRole sysRole) {
        page = sysRoleService.getSysUserPage(page, sysRole);
        return ResponseEntity.ok(page);
    }

    /**
     * 管理员新增中角色显示
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "角色列表")
    @PreAuthorize("hasAuthority('sys:role:list')")
    public ResponseEntity<List<SysRole>> getUserList() {
        List<SysRole> list = sysRoleService.list();
        return ResponseEntity.ok(list);
    }

    @PostMapping()
    @ApiOperation(value = "角色新增")
    @PreAuthorize("hasAuthority('sys:role:save')")
    public ResponseEntity<Void> addRoleAndAuth(@RequestBody SysRole sysRole) {
        sysRoleService.addRoleAndAuth(sysRole);
        return ResponseEntity.ok().build();
    }
}
